import { app, BrowserWindow, Menu, ipcMain } from 'electron'
import path from 'path'
/*
  上一曲
  下一曲
  暂停 | 播放
  退出
*/

let mainWindow;

const contextMenuTemplate: any = [
  { label: '上一曲', click: () => mainWindow.webContents.send('audioPrev') },
  { label: '下一曲', click: () => mainWindow.webContents.send('audioNext') },
  { label: '暂停|播放', click: () => mainWindow.webContents.send('stopOrPlay') },
  { type: 'separator' }, // 分隔线
  { label: '退出', click: () => app.quit() },
]

const contextMenu = Menu.buildFromTemplate(contextMenuTemplate);

app.on('ready', () => {
  mainWindow = new BrowserWindow({
    width: 470,
    height: 140,
    frame: false,
    transparent: true,
    resizable: false,
    alwaysOnTop: true,
    center: true,
    webPreferences: {
      webSecurity: false,
      webviewTag: true,
      spellcheck: false,
      nodeIntegration: true,
      allowRunningInsecureContent: true,
      backgroundThrottling: true, // 提高最小化页面的性能
      contextIsolation: false,
      preload: path.join(__dirname, './preload.js'),
    },
  });
  mainWindow.webContents.openDevTools({ mode: 'detach' })
  mainWindow.loadURL('http://localhost:5173/')

  mainWindow.webContents.on('context-menu', (e) => {
    e.preventDefault();
    contextMenu.popup({ window: mainWindow });
  });
});

